local haloCam = false
local sx, sy = guiGetScreenSize()
local cameraState = nil
local green = tocolor ( 0, 255, 0, 255 )
local yellow = tocolor ( 255, 255, 0, 255 )
local red = tocolor ( 255, 0, 0, 255 )

function resourceStart()
	toggleControl ( "change_camera", false )
	bindKey ( "v", "up", toggleHaloSteer )
end

function resourceStop()
	unbindKey ( "v", "up", toggleHaloSteer )
	toggleControl ( "change_camera", true )
end

function cameraMath()
	local vehicle = getPedOccupiedVehicle ( getLocalPlayer() )
	if vehicle then
		local vx, vy, vz = getElementPosition ( vehicle )
		local vrx, vry, vrz = getVehicleRotation ( vehicle )
		local cx, cy = getCameraMatrix ()
		local dist = 15
		local dist2 = 5
		local rdist = math.abs ( getDistanceBetweenPoints2D ( cx, cy, vx, vy ) )
		local fx, fy = 1, 1
		if rdist < 6 then
			fx, fy = vx + dist2 * math.cos ( math.rad ( vrz + 90 ) ), vy + dist2 * math.sin ( math.rad ( vrz+90 ) )
		else
			fx, fy = vx + dist * math.cos ( math.rad ( vrz + 90 ) ), vy + dist * math.sin ( math.rad ( vrz+90 ) )
		end
		local angle = math.deg ( math.atan2 ( cy - fy, cx - fx ) ) - vrz
		local bdist = math.abs ( getDistanceBetweenPoints2D ( cx, cy, fx, fy ) )
		if angle < -360 then
			angle = angle + 360
		end
		angle = math.abs ( math.round ( angle, 2 ) )
		if angle > 100 and bdist > 3 then
			dxDrawText ( angle.." turning right.", 50, sy - 20, 100, 20, green, 1, "default-bold", "center", "center", false, false, false )
			setControlState ( "vehicle_left", false )
			setControlState ( "vehicle_right", true )
		elseif angle < 80 and bdist > 3 then
			dxDrawText ( angle.." turning left.", 50, sy - 20, 100, 20, green, 1, "default-bold", "center", "center", false, false, false )
			setControlState ( "vehicle_right", false )
			setControlState ( "vehicle_left", true )
		else
			dxDrawText ( angle.." not turning.", 50, sy - 20, 100, 20, red, 1, "default-bold", "center", "center", false, false, false )
			setControlState ( "vehicle_right", false )
			setControlState ( "vehicle_left", false )
		end
		
	end
end

function toggleHaloSteer ()
	if haloCam == false then
		cameraState = getCameraViewMode()
		setControlState ( "vehicle_left", false )
		setControlState ( "vehicle_right", false )
		toggleControl ( "vehicle_left", false )
		toggleControl ( "vehicle_right", false )
		addEventHandler ( "onClientRender", getRootElement(), cameraMath )
		setCameraViewMode ( 3 )
		haloCam = true
	else
		setCameraViewMode ( cameraState )
		setControlState ( "vehicle_left", true )
		setControlState ( "vehicle_right", true )
		toggleControl ( "vehicle_left", true )
		toggleControl ( "vehicle_right", true )
		removeEventHandler ( "onClientRender", getRootElement(), cameraMath )
		haloCam = false
	end
end

function math.round(number, decimals, method)
    decimals = decimals or 0
    local factor = 10 ^ decimals
    if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor
    else return tonumber(("%."..decimals.."f"):format(number)) end
end

addEventHandler ( "onClientResourceStart", resourceRoot, resourceStart )
addEventHandler ( "onClientResourceStop", resourceRoot, resourceStop )